//
//  YYKeychain+SYAdd.m
//  yykeychaine
//
//  Created by huchu on 2018/5/25.
//  Copyright © 2018年 sysjones. All rights reserved.
//

#import "SYKeychain+SYNAdd.h"

@implementation YYKeychain (group)
+(NSString *)appldentifierPrefix{
    YYKeychainItem*item = [YYKeychainItem new];
    item.service=@"";
    item.account=@"appldentifierPrefix";
    item.password=@"appldentifierPrefix";
    YYKeychainItem*resultItem= [YYKeychain selectOneItem:item];
    if (!resultItem) {
        [YYKeychain insertItem:item];
        resultItem= [YYKeychain selectOneItem:item];
    }
    NSArray*a =[resultItem.accessGroup componentsSeparatedByString:@"."];
    return a.firstObject;
}
+(BOOL)setPassword:(NSString *)password forService:(NSString *)serviceName account:(NSString *)account group:(NSString *)group {
    return [self setPassword:password forService:serviceName account:account group:group error:nil];
}
+(BOOL)setPassword:(NSString *)password forService:(NSString *)serviceName account:(NSString *)account group:(NSString *)group error:(NSError * _Nullable __autoreleasing *)error {
    if (!password || !serviceName || !account) {
        NSDictionary *userInfo =  @{ NSLocalizedDescriptionKey : @"One or more parameters passed to a function were not valid" };
        NSError*tmpError= [NSError errorWithDomain:@"com.ibireme.yykit.keychain" code:errSecParam userInfo:userInfo];
        if (error) *error = tmpError;
        return NO;
    }
    YYKeychainItem *item = [YYKeychainItem new];
    item.service = serviceName;
    item.account = account;
    item.accessGroup= group;
    YYKeychainItem *result = [self selectOneItem:item error:NULL];
    if (result) {
        result.password = password;
        return [self updateItem:result error:error];
    } else {
        item.password = password;
        return [self insertItem:item error:error];
    }
}
+(NSString *)getPasswordForService:(NSString *)serviceName account:(NSString *)account group:(NSString *)group{
    return [self  getPasswordForService:serviceName account:account group:group error:nil];
}
+(NSString *)getPasswordForService:(NSString *)serviceName account:(NSString *)account group:(NSString *)group error:(NSError * _Nullable __autoreleasing * _Nullable)error{
    if (!serviceName || !account) {
        NSDictionary *userInfo =  @{ NSLocalizedDescriptionKey : @"One or more parameters passed to a function were not valid" };
        NSError*tmpError= [NSError errorWithDomain:@"com.ibireme.yykit.keychain" code:errSecParam userInfo:userInfo];
        if (error) *error = tmpError;
        return nil;
    }
    YYKeychainItem *item = [YYKeychainItem new];
    item.service = serviceName;
    item.account = account;
    item.accessGroup=group;
    YYKeychainItem *result = [self selectOneItem:item error:error];
    return result.password;
}
+(BOOL)deletePasswordForService:(NSString *)serviceName account:(NSString *)account group:(NSString*)group{
    return [self deletePasswordForService:serviceName account:account group:group error:nil];
}

+(BOOL)deletePasswordForService:(NSString *)serviceName account:(NSString *)account  group:(NSString*)group error:(NSError * _Nullable __autoreleasing *)error{
    if (!serviceName || !account) {
        NSDictionary *userInfo =  @{ NSLocalizedDescriptionKey : @"One or more parameters passed to a function were not valid" };
        NSError*tmpError= [NSError errorWithDomain:@"com.ibireme.yykit.keychain" code:errSecParam userInfo:userInfo];
        if (error) *error = tmpError;
        return NO;
    }
    
    YYKeychainItem *item = [YYKeychainItem new];
    item.service = serviceName;
    item.account = account;
    item.accessGroup=group;
    return [self deleteItem:item error:error];

}

@end
